package com.hnguigu.health.houduan.lcj.controller_lcj;

import com.google.gson.Gson;
import com.hnguigu.health.houduan.lcj.pojo_lcj.PagVo_lcj;
import com.hnguigu.health.houduan.lcj.pojo_lcj.TCheckgroup_lcj;
import com.hnguigu.health.houduan.lcj.pojo_lcj.TCheckitem_lcj;
import com.hnguigu.health.houduan.lcj.pojo_lcj.TSetmealCheckgroup_lcj;
import com.hnguigu.health.houduan.lcj.service_lcj.TCheckgroupService_lcj;
import com.hnguigu.health.houduan.lcj.service_lcj.TCheckitemService_lcj;
import com.hnguigu.health.houduan.lcj.service_lcj.TSetmealCheckgroupService_lcj;
import com.hnguigu.health.pojo.TSportsItemLibrary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/checkgrou")
public class TCheckitemController_lcj {
    @Autowired(required = false)
    TCheckitemService_lcj tcheck;

    @Autowired(required = false)
    TSetmealCheckgroupService_lcj tsmc;

    @Autowired(required = false)
    TCheckgroupService_lcj tcheckgrou;

    @PostMapping("/findList")//分页
    @ResponseBody
    public PagVo_lcj<TCheckitem_lcj> findList(@RequestBody Map<String,String> map){
       return tcheck.findAll1(Integer.valueOf(map.get("page2")),Integer.valueOf(map.get("num2")),map.get("name"));
    }

    @PostMapping("/findList2")
    @ResponseBody
    public List<TCheckitem_lcj> findList2(@RequestBody Map<String,String> map){
        return tcheck.findAll2(map.get("name"));
    }

    @GetMapping("/byId")
    @ResponseBody
    public Integer byId(String code){
        TCheckitem_lcj byid = tcheck.ById(code);
        if(byid==null){
            return 1;
        }else{
            return 2;
        }
    }

    @GetMapping("/delByid")
    @ResponseBody
    public Integer delByid(String checkid){
        List<TSetmealCheckgroup_lcj> bycheckID = tsmc.getBycheckID(checkid);
        System.out.println(checkid);
        if(bycheckID.size()==0){
            tcheck.delById(checkid);
            return 1;
        }else{
            return 2;
        }
    }

    @GetMapping("/addcheck")
    @ResponseBody
    public Integer addcheck(String checkitem){
        Gson gson = new Gson();
        TCheckitem_lcj tCheckitem_lcj = gson.fromJson(checkitem, TCheckitem_lcj.class);
        tCheckitem_lcj.setAge("0-100");
        tCheckitem_lcj.setCheckitem_type("检测项目");
        TCheckitem_lcj lastbean = tcheck.Lastbean();
        String code = lastbean.getCode();
        String newTmid="";
        String substring="";
        if(lastbean==null){
            newTmid="JC001";
        }else{
            substring = code.substring(2, code.length());
        }
        if(Integer.parseInt(substring)<10){
            newTmid= "JC00"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=10){
            newTmid= "JC0"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=100){
            newTmid= "JC"+(Integer.parseInt(substring)+1);
        }
        tCheckitem_lcj.setCode(newTmid);
//        System.out.println(tCheckitem_lcj+"检测项目");
        tcheck.insertSelective(tCheckitem_lcj);
        return 1;
    }

    @GetMapping("/updcheck")
    @ResponseBody
    public Integer updcheck(String tim,String checkitem){
        Gson gson = new Gson();
        TCheckitem_lcj tCheckitem_lcj = gson.fromJson(checkitem, TCheckitem_lcj.class);
        tCheckitem_lcj.setUpdatetime(tim);
        System.out.println(tCheckitem_lcj+"检查项修改");
        tcheck.updateByPrimaryKeySelective(tCheckitem_lcj);
        return 1;
    }
}
